package com.maikang.exammall.entity.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Slice;

import java.util.Iterator;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SlicedContent<T> implements Iterable<T>{

    private List<T> content;
    private Integer size;
    private Integer number;
    private Integer numberOfElements;
    private Boolean first;
    private Boolean last;
    private Boolean empty;

    public SlicedContent(Slice<T> slice) {
        content = slice.getContent();
        size = slice.getSize();
        number = slice.getNumber();
        numberOfElements = slice.getNumberOfElements();
        first = slice.isFirst();
        last = slice.isLast();
        empty = slice.isEmpty();
    }

    @Override
    public Iterator<T> iterator() {
        return new MyIterator();
    }
    class MyIterator implements Iterator<T>{

        /**相当于索引*/
        private int index =0;
        @Override
        public boolean hasNext() {
            //只要在调用next()后，index自加，确保index不等于person的长度
            return index!=content.size();
        }

        @Override
        public T next() {
            //使用索引来获取person数组中的某一项
            return content.get(index++);
        }

        @Override
        public void remove() {

        }

    }
}
